import { ITEM_STORAGE, LocalStorageMgr } from "../components/LocalStorage/LocalStorageMgr";
import { AudioMgr } from "./AudioMgr";

/**用户设置数据*/
export class SettingDataMgr {
    private static _instance:SettingDataMgr;
    private _soundEnabled: boolean;
    private _musicEnabled: boolean;
    private _vibrationEnabled: boolean;
    private _betChipID:number;//用户选择的下注筹码id
    public static get instance():SettingDataMgr{
        if(this._instance==null)
        {
            this._instance = new SettingDataMgr();
        }
        return this._instance;
    }
    constructor() {
        // 初始化时从本地存储获取设置信息，如果不存在则使用默认值
        this._soundEnabled = this._loadSetting(ITEM_STORAGE.SOUND_ENABLED, true);
        this._musicEnabled = this._loadSetting(ITEM_STORAGE.MUSIC_ENABLED, true);
        this._vibrationEnabled = this._loadSetting(ITEM_STORAGE.VIBRATION_ENABLED, true);
        this._betChipID = this._loadSetting(ITEM_STORAGE.BET_CHIP_ID,0);
    }
    // 获取本地存储的设置，如果不存在则返回默认值
    private _loadSetting(key: ITEM_STORAGE, defaultValue: any): any {
        const storedValue = LocalStorageMgr.instance.getItem(key);
        if (storedValue !== null) {
            return JSON.parse(storedValue);
        }
        return defaultValue;
    }

    // 保存设置到本地存储
    private _saveSetting(key: ITEM_STORAGE, value: any): void {
        LocalStorageMgr.instance.setItem(key, JSON.stringify(value));
    }

    // 获取声音设置
    get soundEnabled(): boolean {
        return this._soundEnabled;
    }

    // 设置声音设置，并保存到本地存储
    set soundEnabled(enabled: boolean) {
        this._soundEnabled = enabled;
        this._saveSetting(ITEM_STORAGE.SOUND_ENABLED, enabled);
    }

    // 获取背景音乐设置
    get musicEnabled(): boolean {
        return this._musicEnabled;
    }

    // 设置背景音乐设置，并保存到本地存储
    set musicEnabled(enabled: boolean) {
        this._musicEnabled = enabled;
        this._saveSetting(ITEM_STORAGE.MUSIC_ENABLED, enabled);
        if(this._musicEnabled)
        {
            AudioMgr.instance.resumeBgMusic();
        }else{
            AudioMgr.instance.stopBgMusic();
        }
    }

    // 获取震动设置
    get vibrationEnabled(): boolean {
        return this._vibrationEnabled;
    }

    // 设置震动设置，并保存到本地存储
    set vibrationEnabled(enabled: boolean) {
        this._vibrationEnabled = enabled;
        this._saveSetting(ITEM_STORAGE.VIBRATION_ENABLED, enabled);
    }
    set betChipID(chipID:number){
        this._betChipID = chipID;
        this._saveSetting(ITEM_STORAGE.BET_CHIP_ID, this._betChipID);
    }
    get betChipID():number{
        return this._betChipID;
    }
}


